package com.sand.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sand.Exception.SandException;
import com.sand.dto.Owner.ExecutionDelete;
import com.sand.dto.Owner.MerchantOrOwner;
import com.sand.dto.Select.UserSelect;
import com.sand.dto.merchant.merchantDto;
import com.sand.entity.buy.execution;
import com.sand.entity.user.User;
import com.sand.feign.MerchantFeign;
import com.sand.feign.UserFeign;
import com.sand.mapper.ExecutionMapper;
import com.sand.result.AppHttpCodeEnum;
import com.sand.result.ResponseResult;
import com.sand.service.ExecutionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ExecutionServiceImpl extends ServiceImpl<ExecutionMapper, execution> implements ExecutionService {

    @Autowired
    private MerchantFeign merchantFeign;

    @Autowired
    private UserFeign userFeign;

    /**
     * 添加方案预执行
     *
     * @param execution
     */
    @Override
    public Integer add(execution execution) {

        //参数校验
        if (execution.getMerchantid() == null) {
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID, "没有传入设计人");
        }

        if (execution.getOwnerid() == null) {
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID, "没有传入委托人");
        }

        if (execution.getSchemeid() == null) {
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID, "没有传入方案");
        }

        //判断设计人
        panMerchant(execution.getMerchantid());

        //判断委托人
        panOwner(execution.getOwnerid());

        if (execution.getMeet() == null) {
            //默认约定2天之内完成方案预执行
            execution.setMeet(LocalDateTime.now().plusDays(2).toString());
        }

        //新增
        save(execution);

        return 1;

    }


    /**
     * 查询与该商家.客户相关的方案预执行
     *
     * @param merchantOrOwner
     */
    @Override
    public List<execution> selectById(MerchantOrOwner merchantOrOwner) {

        //获取设计人id
        Integer mercahntid = merchantOrOwner.getMerchantid();

        //获取委托人id
        Integer ownerid = merchantOrOwner.getOwnerid();

        if (mercahntid == null && ownerid == null) {
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID, "未传入要查寻id");
        }

        //构造查询条件
        LambdaQueryWrapper<execution> wrapper = new LambdaQueryWrapper<>();

        //两个都传入
        if (mercahntid != null && ownerid != null) {
            panMerchant(mercahntid);
            panOwner(ownerid);

            wrapper.eq(execution::getMerchantid, mercahntid).eq(execution::getOwnerid, ownerid);

            return list(wrapper);
        }


        //只传入商家id
        if (mercahntid != null) {
            //设计人判断
            panMerchant(mercahntid);

            wrapper.eq(execution::getMerchantid, mercahntid);

            return list(wrapper);
        }

        //只传入客户id

        panOwner(ownerid);

        wrapper.eq(execution::getOwnerid, ownerid);

        return list(wrapper);

    }




    /**
     * 修改方案预执行
     *
     * @param execution
     * @return
     */
    @Override
    public Integer upd(execution execution) {

        if (execution.getSchemeid() == null) {
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID, "没有传入方案");
        }

        if (execution.getMerchantid() == null && execution.getOwnerid() == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入操作人");
        }

        //根据id修改
        if (execution.getId() != null){
            //判断是否存在
            execution cc = getById(execution.getId());

            if (cc == null){
                throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"传入的修改的方案预执行的id有误");
            }

            //只修改时间
            cc.setMeet(execution.getMeet());

            updateById(cc);

            return 1;
        }



        //根据方案id修改
        execution one = getOne(new LambdaQueryWrapper<execution>().eq(com.sand.entity.buy.execution::getSchemeid, execution.getSchemeid()));

        //只修改时间
        one.setMeet(execution.getMeet());

        updateById(one);

        return 1;
    }

    /**
     * 删除方案预执行
     *
     * @param executionDelete
     */
    @Override
    public Integer down(ExecutionDelete executionDelete) {

        //表id
        Integer executionid = executionDelete.getExecutionid();

        //方案表id
        Integer schemeid = executionDelete.getSchemeid();


        if (executionid == null && schemeid == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"未传入id");
        }

        if (executionid != null && schemeid != null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"不能同时传入两个id");
        }

        //根据预执行id删除
        if (executionid != null){

            execution execution = getById(executionid);

            if (execution == null){
                throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"删除的id不存在");
            }

            //删除
            removeById(executionid);

            return 1;
        }

        //根据方案表id删除

        execution one = getOne(new LambdaQueryWrapper<execution>().eq(execution::getSchemeid, schemeid));

        if (one == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"传入的方案id有误");
        }

        //删除
        removeById(one.getId());

        return 1;
    }





    //判断设计人
    private void panOwner(Integer id) {
        if (id == null) {
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID, "为传入方案委托人");
        } else {
            ResponseResult<User> inquire = userFeign.inquire(new UserSelect(id, null));

            if (inquire.getData() == null) {
                throw new SandException(AppHttpCodeEnum.PARAM_INVALID, "传入的委托人不存在");
            }
        }
    }

    //判断商家
    private void panMerchant(Integer id) {
        if (id == null) {
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID, "为传入方案设计人");
        } else {
            merchantDto merchantDto = merchantFeign.select(new UserSelect(id, null)).getData();

            if (merchantDto == null) {
                throw new SandException(AppHttpCodeEnum.PARAM_INVALID, "添加的设计人不存在");
            }

            if (merchantDto.getPermission() == 0) {
                throw new SandException(AppHttpCodeEnum.PARAM_INVALID, "添加的设计人权限不够");
            }

        }
    }


}
